﻿using BLL;
using log4net;
using SysFramework;
using System.Security.Cryptography;
using Tools;

namespace YunProject.Base
{
    /// <summary>
    /// 定时执行任务方法类
    /// </summary>
    public class AutoTask:IHostedService, IDisposable
    {
        private Timer? timer;

        AutoTaskBLL autoBLL = new AutoTaskBLL();
        public ILog log = LogManager.GetLogger(typeof(AutoTask));
        private void AutoSyncData(object state)
        {
            log.Info("开始执行同步任务");
            if (AppSettingHelper.ReadAppSettings("ConntectERP") != "1")
            {
                log.Info("系统没有开启对接ERP功能，自动同步任务停止运行") ;
            }
            else
            {
                //autoBLL.auto_User();//人员
                //autoBLL.auto_Dept();//部门
                //autoBLL.sync_Inventory();//存货
                //autoBLL.auto_WareHouse();//仓库
                //autoBLL.sync_InventoryClass();//存货大类
                //autoBLL.auto_Customer();//客户
                //autoBLL.auto_Vendor();//供应商
                //autoBLL.sync_BanGunData();//版辊
                autoBLL.sync_BanGunIn();//版辊入库
            }
           
        }
        public void Dispose()
        {
            timer?.Dispose();
        }
        /// <summary>
        /// Triggered when the application host is ready to start the service.
        /// </summary>
        /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
        public Task StartAsync(CancellationToken cancellationToken)
        {
 
            var sjjg=AppSettingHelper.ReadAppSettings("AutoTask", "TimeSpan");
            var isSync = AppSettingHelper.ReadAppSettings("AutoTask", "IsSync");
            if (isSync == "1") {
                timer = new Timer(AutoSyncData, null, TimeSpan.Zero, TimeSpan.FromSeconds(60 * int.Parse(sjjg)));
            }
            return Task.CompletedTask;

        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            log.Info("结束自动同步任务");
            return Task.CompletedTask;

        }
    }
}
